package com.cssy.xxkj.logic.web;

import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONArray;
import com.cssy.authority.service.IScUserRolewhPlaceService;
import com.cssy.base.web.BaseController;
import com.cssy.base.web.BodyMsg;
import com.cssy.xxkj.logic.contants.BillStatusUtil;
import com.cssy.xxkj.logic.dao.LogicCheckBackItemDAO;
import com.cssy.xxkj.logic.entity.LogicCheckBackDO;
import com.cssy.xxkj.logic.entity.LogicCheckBackItemDO;
import com.cssy.xxkj.logic.entity.LogicProductionStorageoutDO;
import com.cssy.xxkj.logic.entity.LogicWarehouseOtheroutDO;
import com.cssy.xxkj.logic.service.ILogicCheckBackService;
import com.cssy.xxkj.logic.service.ILogicProductionStorageOutService;
import com.cssy.xxkj.logic.service.ILogicWareHouseOtherOutService;
import com.cssy.yd.logic.util.SqlUtil;
import com.plat.exception.SyBaseException;
import com.plat.helper.JqGridHandler;
import com.plat.helper.SearchCondition;
import com.plat.helper.SearchConditions;
import com.plat.helper.SearchResult;
import com.plat.util.GlobalResLoader;
import com.plat.util.OperationFlag;
import com.plat.util.SessionUtil;
import com.plat.util.json.OutJsonGrid;
/**
 * 反审
 * @author lb
 *
 */
@RequestMapping("/logic/checkback")
@Controller
public class CheckBackController extends BaseController<LogicCheckBackDO> {

	public static final String MANAGE="/xxkj/logic/checkback/checkBackManager";

	public static final String EIDT="/xxkj/logic/checkback/checkBackOperation";

	@Resource(name="logicCheckBackService")
	private ILogicCheckBackService logicCheckBackService;

	@Resource(name="scUserRolePlaceService")
	private IScUserRolewhPlaceService scUserRolePlaceService;

	@Resource(name="logicCheckBackItemDAO")
	private LogicCheckBackItemDAO logicCheckBackItemDAO;

	@Resource(name="logicWareHouseOtherOutService")
	private ILogicWareHouseOtherOutService logicWareHouseOtherOutService;
	
	@Resource(name="logicProductionStorageOutService")
	ILogicProductionStorageOutService logicProductionStorageOutService;
	@Override
	public String manager(HttpServletRequest request, Model model)
			throws Exception {
		String type=request.getParameter("type");
		model.addAttribute("type", type);
		model.addAttribute("wh",SqlUtil.sqlUtil.roleWH(request, scUserRolePlaceService));
		return MANAGE;
	}

	@Override
	public void list(HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		String type=request.getParameter("type");
		JqGridHandler hander = new JqGridHandler(request);
		SearchConditions scs = new SearchConditions();
		hander.getWheres(scs);
		scs.addCondition("zd008Type", type,SearchCondition.OP_EQUAL);
		SearchResult result =logicCheckBackService.findListByExample(scs);
		OutJsonGrid out = new OutJsonGrid(result);
		out.printJSon(response);
	}

	@Override
	public String operationForm(HttpServletRequest request,@PathVariable String id,
			@PathVariable String flag, Model model) throws Exception {
		String type=request.getParameter("type");
		model.addAttribute("type", type);
		if (OperationFlag.ADD.equals(flag)) {
			model.addAttribute("flag", "ADD");
		} else {
			LogicCheckBackDO checkBack = logicCheckBackService.findListById(id);
			model.addAttribute("cb", checkBack);
		}
		return EIDT;
	}

	@Override
	public Map<String, Object> save(HttpServletRequest request,
			LogicCheckBackDO curAP, BindingResult result, Model model)
					throws Exception {
		BodyMsg bodyMap = BodyMsg.getInstance();
		String itemdata = request.getParameter("addressInfos");
		if(itemdata==null&&"".equals(itemdata)){
			bodyMap.isFail();
			bodyMap.configWebInfor("物料不能为空！");
			return bodyMap.modelMap;
		}
		try {
			logicCheckBackService.cbSave(curAP, request);
			bodyMap.isSuccess();
			bodyMap.configWebInfor(GlobalResLoader.getOpsuccess());
		} catch (SyBaseException e) {
			e.printStackTrace();
			bodyMap.isFail();
			bodyMap.configWebInfor(GlobalResLoader.getOpfailed()
					+ e.getMessage());
		}
		return bodyMap.modelMap;
	}
	@RequestMapping(value = "/updateState")
	@ResponseBody
	public Map<String,String> updateState(HttpServletRequest request,
			HttpServletResponse response, Model model) throws Exception {
		Map<String, String> map=new HashMap<String, String>();
		try{
			String id = request.getParameter("id");
			if(id!=null&&!"".equals(id)){
				LogicCheckBackDO ld=logicCheckBackService.findListById(id);
				if(ld!=null&&ld.getId()!=null){
					if(ld.getZd011Stauts().equals(BillStatusUtil.CHECK_BACK_ONE)){
						ld.setZd011Stauts(BillStatusUtil.CHECK_BACK_TWO);
						logicCheckBackService.update(ld, SessionUtil.getUser(request));
					}else{
						throw new SyBaseException("驳回失败！！");
					}
				}
				map.put("msg", "SUCCESS");
			}else{
				map.put("msg", "ERROR");
			}
		}catch(Exception e){
			e.printStackTrace();
			map.put("msg", "ERROR");
		}
		return map;
	}

	@Override
	public Map<String, Object> delete(HttpServletRequest request,
			HttpServletResponse response, Model model) throws Exception {
		BodyMsg bodyMap = BodyMsg.getInstance();
		try {
			String ids = request.getParameter("ids");
			String[] fields = ids.split(",");
			for (String field : fields) {
				if(StringUtils.isBlank(field)){
					continue;
				}else{
					logicCheckBackService.deleteBizRemoved(field);
				}
			}
			bodyMap.isSuccess(GlobalResLoader.getOpsuccess());
		} catch (Exception e) {
			e.printStackTrace();
			bodyMap.isFail(GlobalResLoader.getOpfailed() + e.getMessage());
		}
		return bodyMap.modelMap;
	}

	@RequestMapping("/griditems")
	public void griditems(HttpServletRequest request,HttpServletResponse response) throws Exception{
		String id = request.getParameter("id");
		String type=request.getParameter("type");
		List<LogicCheckBackItemDO> list= new ArrayList<LogicCheckBackItemDO>();
		if(StringUtils.isNotBlank(id)){
			list= logicCheckBackService.getCheckBackItemList(id, type);
		}
		OutJsonGrid out = new OutJsonGrid(list);
		out.printJSon(response);
	} 

	@RequestMapping("/gridCBitems")//获得反审记录表的行记录
	public void gridCBitems(HttpServletRequest request,HttpServletResponse response) throws Exception{
		String id = request.getParameter("id");
		List<LogicCheckBackItemDO> list= new ArrayList<LogicCheckBackItemDO>();
		if(StringUtils.isNotBlank(id)){
			LogicCheckBackItemDO lc= new LogicCheckBackItemDO();
			lc.setZd001CkeckBackId(id);
			list= logicCheckBackItemDAO.findListByExample(lc);
		}
		OutJsonGrid out = new OutJsonGrid(list);
		out.printJSon(response);
	} 
	@RequestMapping("/chkeckObjectInfo")
	public void otherOutInfo(HttpServletRequest request,HttpServletResponse response)throws Exception{
		String type=request.getParameter("type");
		String id= request.getParameter("id");
		Map<String, Object> map = new HashMap<String, Object>();
		if(BillStatusUtil.CHECK_BACK_OTHEROUT.equals(type)){
			LogicWarehouseOtheroutDO ld=logicWareHouseOtherOutService .findListById(id);
			map.put("outdate", ld.getZd010OtherDate());
			map.put("aegnt", ld.getZd006Aegnt());
		}else if (BillStatusUtil.CHECK_BACK_SALESOUT.endsWith(type)) {
			LogicProductionStorageoutDO ld= logicProductionStorageOutService.findListById(id);
			map.put("outdate", ld.getZd010OtherDate());
			map.put("aegnt", ld.getZd006Aegnt());
		}
		PrintWriter pw=response.getWriter();
		pw.write(JSONArray.toJSONString(map));
		if(pw!=null){
			pw.flush();
			pw.close();
		}
	}
}
